package com.atguigu.daijia.customer.service.impl;

import com.atguigu.daijia.common.constant.RedisConstant;
import com.atguigu.daijia.common.execption.GuiguException;
import com.atguigu.daijia.common.result.Result;
import com.atguigu.daijia.common.result.ResultCodeEnum;
import com.atguigu.daijia.customer.client.CustomerInfoFeignClient;
import com.atguigu.daijia.customer.service.CustomerService;
import com.atguigu.daijia.model.form.customer.UpdateWxPhoneForm;
import com.atguigu.daijia.model.vo.customer.CustomerInfoVo;
import com.atguigu.daijia.model.vo.customer.CustomerLoginVo;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class CustomerServiceImpl implements CustomerService {

    @Resource
    private CustomerInfoFeignClient customerInfoFeignClient;

    @Resource
    private RedisTemplate redisTemplate;

    /**
     * 小程序授权登录
     *
     * @param code 小程序授权码
     */
    @Override
    public String login(String code) {
        // 1 拿着code进行远程调用，返回用户id
        Result<Long> login = customerInfoFeignClient.login(code);

        // 2 判断如果返回失败了，返回错误提示
        if (login.getCode() != 200) {
            throw new GuiguException(ResultCodeEnum.DATA_ERROR);
        }

        // 3 获取远程调用返回用户id
        Long customerId = login.getData();

        // 4 判断返回用户id是否为空，如果为空，返回错误提示
        if (customerId == null) {
            // 后续完善，新添加用户逻辑
            throw new GuiguException(ResultCodeEnum.DATA_ERROR);
        }

        // 5 生成token字符串
        String token = UUID.randomUUID().toString().replaceAll("-", "");

        // 6 把用户id放到Redis，设置过期时间
        // key:token  value:customerId
        // redisTemplate.opsForValue().set(token,customerId.toString(),30, TimeUnit.MINUTES);
        redisTemplate.opsForValue().set(RedisConstant.USER_LOGIN_KEY_PREFIX + token,
                customerId.toString(),
                RedisConstant.USER_LOGIN_KEY_TIMEOUT,
                TimeUnit.MINUTES);

        // 7 返回token
        return token;
    }

    /**
     * 获取客户登录信息
     *
     * @param token 令牌
     */
    @Override
    public CustomerLoginVo getCustomerLoginInfo(String token) {
        // 2 根据token查询redis
        // 3 查询token在redis里面对应用户id
        String customerId = (String) redisTemplate.opsForValue().get(RedisConstant.USER_LOGIN_KEY_PREFIX + token);

        if (!StringUtils.hasText(customerId)) {
            throw new GuiguException(ResultCodeEnum.DATA_ERROR);
        }

        // 4 根据用户id进行远程调用,得到用户信息
        Result<CustomerLoginVo> customerLoginVoResult = customerInfoFeignClient.getCustomerInfo(Long.parseLong(customerId));

        if (customerLoginVoResult.getCode() != 200) {
            throw new GuiguException(ResultCodeEnum.DATA_ERROR);
        }

        CustomerLoginVo customerLoginVo = customerLoginVoResult.getData();
        if (customerLoginVo == null) {
            throw new GuiguException(ResultCodeEnum.DATA_ERROR);
        }

        return customerLoginVo;
    }

    /**
     * 更新客户微信手机号码
     *
     * @param updateWxPhoneForm 更新客户手机号码表单数据
     */
    @Override
    public Boolean updateWxPhoneNumber(UpdateWxPhoneForm updateWxPhoneForm) {
        customerInfoFeignClient.updateWxPhoneNumber(updateWxPhoneForm);

        return true;
    }

}
